package io.finer.erp.jeecg.purchase.service.impl;

import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.IService;
import io.finer.erp.jeecg.approve.service.impl.ApproveServiceImpl;
import io.finer.erp.jeecg.bas.entity.BasCurrency;
import io.finer.erp.jeecg.bas.mapper.BasCurrencyMapper;
import io.finer.erp.jeecg.purchase.entity.PurchaseRequest;
import io.finer.erp.jeecg.purchase.entity.PurchaseRequestLine;
import io.finer.erp.jeecg.purchase.mapper.PurchaseRequestLineMapper;
import io.finer.erp.jeecg.purchase.mapper.PurchaseRequestMapper;
import io.finer.erp.jeecg.purchase.service.PurchaseRequestService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import io.finer.erp.jeecg.purchase.vo.PurchaseOrderPage;
import io.finer.erp.jeecg.purchase.vo.PurchaseRequestPage;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.io.Serializable;
import java.util.Collection;
import java.util.List;

/**
 * <p>
 * 采购申请 服务实现类
 * </p>
 *
 * @author yaoyifan
 * @since 2021-04-12
 */
@Service
public class PurchaseRequestServiceImpl extends ApproveServiceImpl<PurchaseRequestMapper, PurchaseRequest> implements PurchaseRequestService {

    @Autowired
    PurchaseRequestLineMapper purchaseRequestLineMapper;

    @Autowired
    BasCurrencyMapper basCurrencyMapper;

    @Override
    public IPage<PurchaseRequestPage> selectPurchaseRequestPage(Page page, Wrapper<PurchaseRequestPage> queryWrapper) {
        return this.baseMapper.selectPurchaseRequestPage(page, queryWrapper);
    }

    @Override
    public PurchaseRequestPage selectPurchaseRequestPageById(String id) {
        return this.baseMapper.selectPurchaseRequestPageById(id);
    }

    @Override
    @Transactional
    public void saveMain(PurchaseRequest purchaseRequest, List<PurchaseRequestLine> purchaseRequestLineList) {
        purchaseRequest.setBillProcStatus("create");
        this.baseMapper.insert(purchaseRequest);
        if(purchaseRequestLineList != null && purchaseRequestLineList.size() > 0){
            QueryWrapper<BasCurrency> queryWrapper = new QueryWrapper<>();
            String currencyId = basCurrencyMapper.selectOne(queryWrapper.eq("code", "CNY")).getId();
            for(PurchaseRequestLine entity: purchaseRequestLineList){
                entity.setMid(purchaseRequest.getId());
                entity.setCurrencyId(currencyId);
                purchaseRequestLineMapper.insert(entity);
            }
        }
    }

    @Override
    @Transactional
    public void updateMain(PurchaseRequest purchaseRequest, List<PurchaseRequestLine> purchaseRequestLineList) {
        this.baseMapper.updateById(purchaseRequest);
        QueryWrapper<PurchaseRequestLine> queryWrapper = new QueryWrapper<>();
        purchaseRequestLineMapper.delete(queryWrapper.eq("mid",purchaseRequest.getId()));
        if (purchaseRequestLineList != null && purchaseRequestLineList.size() > 0){
            QueryWrapper<BasCurrency> currencyQueryWrapper = new QueryWrapper<>();
            String currencyId = basCurrencyMapper.selectOne(currencyQueryWrapper.eq("code", "CNY")).getId();
            for(PurchaseRequestLine entity: purchaseRequestLineList){
                entity.setMid(purchaseRequest.getId());
                entity.setCurrencyId(currencyId);
                purchaseRequestLineMapper.insert(entity);
            }
        }
    }

    @Override
    @Transactional
    public void delMain(String id) {
        QueryWrapper<PurchaseRequestLine> queryWrapper = new QueryWrapper<>();
        purchaseRequestLineMapper.delete(queryWrapper.eq("mid",id));
        this.baseMapper.deleteById(id);
    }

    @Override
    @Transactional
    public void delBatchMain(Collection<? extends Serializable> idList) {
        for(Serializable id:idList) {
            QueryWrapper<PurchaseRequestLine> queryWrapper = new QueryWrapper<>();
            purchaseRequestLineMapper.delete(queryWrapper.eq("mid",id.toString()));
            this.baseMapper.deleteById(id.toString());
        }
    }

    @Override
    @Transactional
    public void submit(String model, String id) {
        super.submit(model, id, this);
        PurchaseRequest purchaseRequest = new PurchaseRequest();
        purchaseRequest.setId(id);
        purchaseRequest.setBillProcStatus("submit");
        this.baseMapper.updateById(purchaseRequest);
    }

    @Override
    @Transactional
    public void reject(String model, String id, String note) {
        super.reject(model, id, note);
        PurchaseRequest purchaseRequest = new PurchaseRequest();
        purchaseRequest.setId(id);
        purchaseRequest.setBillProcStatus("reject");
        this.baseMapper.updateById(purchaseRequest);
    }

    @Override
    @Transactional
    public String approve(String model, String id, String note) {
        String approveInstanceStatus = super.approve(model, id, note);
        PurchaseRequest purchaseRequest = new PurchaseRequest();
        purchaseRequest.setId(id);
        purchaseRequest.setBillProcStatus(approveInstanceStatus);
        this.baseMapper.updateById(purchaseRequest);
        return approveInstanceStatus;
    }

    @Override
    @Transactional
    public void withDraw(String model, String id) {
        super.withDraw(model, id);
        PurchaseRequest purchaseRequest = new PurchaseRequest();
        purchaseRequest.setId(id);
        purchaseRequest.setBillProcStatus("create");
        this.baseMapper.updateById(purchaseRequest);
    }

    @Transactional
    public void invalid(String id, String note) {
        PurchaseRequest purchaseRequest = new PurchaseRequest();
        purchaseRequest.setId(id);
        purchaseRequest.setBillProcStatus("invalid");
        purchaseRequest.setInvalidationReason(note);
        this.baseMapper.updateById(purchaseRequest);
    }
}
